/**
 * 
 */
package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;

import domain.Article;
import domain.Computer;
import domain.Printer;
import domain.Product;
import domain.Server;
import domain.Supplier;

/**
 * @author grupo22
 *
 */
public class JDBCProductDAO implements IProductDAO {
	
	public Collection selectAllProducts(){
		Collection result = new LinkedList();
		
		Connection con = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
        	String sql = "SELECT * FROM Product;";
        	stmt = con.prepareStatement(sql);
            
            rs = stmt.executeQuery();
            System.out.println("Sentencia: " + stmt.toString());

            while (rs.next()) {
            	String OIDProduct = new String(rs.getString("OIDProduct"));
            	String brand = new String(rs.getString("brand"));
            	String model = new String(rs.getString("model"));
            	double price = new Double(rs.getDouble("price")).intValue();
            	Boolean isValid = new Boolean(rs.getBoolean("isValid"));
            	Boolean validate = new Boolean(rs.getBoolean("validate"));
            	String OIDArticle = new String(rs.getString("OIDArticle"));
            	String OIDSupplier = new String(rs.getString("OIDSupplier"));
            	IArticleDAO artdao = new JDBCArticleDAO();
            	Article article = artdao.select(con, OIDArticle);
            	ISupplierDAO supdao = new JDBCSupplierDAO();
            	Supplier supplier = supdao.select(con, OIDSupplier);
            	//Obtenemos el tipo de producto
            	String objectType = new String(rs.getString("objectType"));
            	//Si es un ordenador
            	if (objectType.compareToIgnoreCase("computer") == 0){
            		Computer c = new Computer(brand, model, price, article, supplier);
            		IComputerDAO compdao = new JDBCComputerDAO();
            		c = compdao.select(con, OIDProduct, c);
            		c.setValid(isValid);
            		c.setValidate(validate);
            		result.add(c);
            	} else if (objectType.compareToIgnoreCase("server") == 0){
            		Server s = new Server(brand, model, price, article, supplier);
            		IServerDAO servdao = new JDBCServerDAO();
            		s.setValid(isValid);
            		s.setValidate(validate);
            		s = servdao.select(con, OIDProduct, s);
            		result.add(s);
            	} else if (objectType.compareToIgnoreCase("printer") == 0){
            		IPrinterDAO servdao = new JDBCPrinterDAO();
            		Printer p = servdao.select(con, OIDProduct);
            		p.setArticle(article);
            		p.setSupplier(supplier);
            		p.setBrand(brand);
            		p.setModel(model);
            		p.setPrice(price);
            		p.setValid(isValid);
            		p.setValidate(validate);
            		result.add(p);
            	}
            	//Product p = new Product(brand, model, price, article, supplier);
            	//result.add(p);
            }
        } catch (SQLException e) {
            System.out.println("Message: Fallo en selectAllProducts" + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(con);
        	try {
                    if (rs != null)
                            rs.close();
                    if (stmt != null)
                            stmt.close();
            } catch (SQLException e) {}
        }

		return result;
	}

	@Override
	public void updateProduct(Product p) {
		Connection con = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        try {
        	System.out.println("Actualizando producto.");
        	String sql = "UPDATE Product SET brand=?, model=?, price=?, isValid=?, validate=?, numVotes=? WHERE brand = ? and model = ?;";
        	stmt = con.prepareStatement(sql);
        	stmt.setString(1, p.getBrand());
        	stmt.setString(2, p.getModel());
        	stmt.setDouble(3, p.getPrice());
        	stmt.setBoolean(4, p.isValid());
        	stmt.setBoolean(5, p.isValidate());
        	stmt.setInt(6, p.getNumVotes());
        	stmt.setString(7, p.getBrand());
        	stmt.setString(8, p.getModel());
        	System.out.println("Sentencia: " + stmt.toString());
        	
        	
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: Fallo en updateProduct " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(con);
        	try {
        		if (stmt != null)
        			stmt.close();
            } catch (SQLException e) {}
        }
		
	}
}
